package core {
	import flash.utils.Dictionary;

	public class GameObject {
		private var interface2components:Dictionary = new Dictionary(false);

		public function GameObject(components:Vector.<Component>) {
			for each (var component:Component in components) {
				for each (var componentInterface:Class in component.componentInterfaces) {
					if (!interface2components[componentInterface]) {
						interface2components[componentInterface] = new Vector.<Component>();
					}
					interface2components[componentInterface].push(component);
				}
			}
		}

		public function component(componentClass:Class):Component {
			if (interface2components[componentClass]) {
				return interface2components[componentClass];
			}
			throw new Error();
		}

		public function adapt(componentInterface:Class):Object {
			var components:Vector.<Component> = interface2components[componentInterface];
			if (!components) {
				throw new Error();
			}
			if (components.length > 1) {
				throw new Error();
			}
			return components[0];
		}
	}
}
